package day2;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
/**
 * Servlet implementation class StoreData
 */
@WebServlet(name = "StoreData", urlPatterns = { "/StoreData" })
public class StoreData extends HttpServlet {
	private static final long serialVersionUID = 1L;

	Connection con;
	Context ctx;
	DataSource ds;

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.write("Server Message: ");
		Connection con;
		String name = request.getParameter("EmployeeName");
		String location = request.getParameter("Location");
		int id = Integer.parseInt(request.getParameter("LanID"));
		try {
			/*
			 * Oracle JDBC connection
			 * Class.forName("oracle.jdbc.driver.OracleDriver"); con =
			 * DriverManager.getConnection(
			 * "jdbc:oracle:thin:@10.23.0.100:1521:esgdb10", "scott", "tiger");
			 */
			/*
			 * PostgreSQL JDBC connection
			 * Class.forName("org.postgresql.Driver"); con =
			 * DriverManager.getConnection(
			 * "jdbc:postgresql://localhost:5432/jee", "karthik", "");
			 */
			// JNDI Pooled Connection

			ctx = new InitialContext();
			ds = (DataSource) ctx.lookup("jdbc/mydata");
			con = ds.getConnection();

			PreparedStatement p = con
					.prepareStatement("INSERT INTO EmpData107811 Values(?,?,?)");
			p.setString(1, name);
			p.setInt(2, id);
			p.setString(3, location);
			p.executeUpdate();
			out.write("Data Succcessfully stored.<br>");
			con.close();
			p.close();
		} catch (Exception e) {
			out.write(e.toString() + ".<br>");
		}
		out.close();
	}
}